#include <stdio.h>
#include <string.h>
#include <symtab.h>
#include <keywords.h>

#define MAXSYMTBENTRIES 0x10000
int symtab[MAXSYMTBENTRIES][4];
int symtab_nextentry = 1;

#define MAXLEXTAPESIZ   0x100000
char lextape[MAXLEXTAPESIZ];
int lextape_nextentry = 0;

int symtab_insert(char const *symbol, int dtype, int lexlevel)
{
  symtab[symtab_nextentry][0] = lextape_nextentry;
  symtab[symtab_nextentry][1] = dtype;
  symtab[symtab_nextentry][2] = lexlevel;
  strcpy(lextape + lextape_nextentry,symbol);
  lextape_nextentry += strlen(symbol) + 1;
  return symtab_nextentry++;
}

int symtab_lookup(char const *symbol, int lexlevel)
{
  int i;
  for(i=symtab_nextentry-1;i>0; i--){
    if(strcmp(lextape+symtab[i][0],symbol)==0 && symtab[i][2] == lexlevel) return i;
  }
  return 0;
}

void print_symtab(void){
  int i;
  printf("symbol table:\n");
  
  for(i=1;i < symtab_nextentry; i++){
    printf("symbol: %s",(lextape+symtab[i][0]));
    switch(symtab[i][1]){
      case INTEGER: printf(" - type: INTEGER");
		    break;
      case REAL: printf(" - type: REAL");
		 break;
      case PROCEDURE: printf(" - type: PROCEDURE");
		      break;
    }
    printf(" - lexical level: %d\n",symtab[i][2]);
  }
}